const mongoose = require('mongoose')
const boom = require('boom')

const Shelf = require('../models/shelf')

const environment = process.env.NODE_ENV
const stage = require('../config')[environment]

module.exports = {
  create: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 201
        const {
          user,
          book
        } = req.body
        const shelf = new Shelf({
          user,
          book
        })
        shelf.save((err, shelf) => {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = shelf
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  delete: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Shelf.findByIdAndDelete(id).
        exec(function (err, shelf) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = shelf
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  update: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Shelf.findByIdAndUpdate(id, req.body).
        exec(function (err, shelf) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = shelf
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  show: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Shelf.findById(id).
        populate('book').
        exec(function (err, shelf) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          shelf.book.cover_path = `${process.env.UPLOAD_URL}${shelf.book.cover_path}`
          shelf.book.file_path = `${process.env.UPLOAD_URL}${shelf.book.file_path}`
          result.status = status
          result.data = shelf
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  index: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        let {
          user,
          book,
        } = req.query
        let condition = {}
        if (user) {
          condition.user = user
        }
        if (book) {
          condition.book = book
        }
        Shelf.find(condition).
        populate('book').
        exec(function (err, shelfs) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          shelfs.forEach(shelf => shelf.book.cover_path = `${process.env.UPLOAD_URL}${shelf.book.cover_path}`)
          shelfs.forEach(shelf => shelf.book.file_path = `${process.env.UPLOAD_URL}${shelf.book.file_path}`)
          result.status = status
          result.data = shelfs
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  }
}
